package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IEmpTrainRequireDAO;
import org.jsola.hr.dto.EmpTrainRequireAddDTO;
import org.jsola.hr.dto.EmpTrainRequireUpdateDTO;
import org.jsola.hr.entity.CompanyDO;
import org.jsola.hr.entity.EmpTrainRequireDO;
import org.jsola.hr.query.EmpTrainRequireQuery;
import org.jsola.hr.service.ICompanyService;
import org.jsola.hr.service.IEmpTrainRequireService;
import org.jsola.hr.vo.EmpTrainRequireListVO;
import org.jsola.hr.vo.EmpTrainRequireVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrEmpTrainRequireServiceImpl")
public class EmpTrainRequireServiceImpl implements IEmpTrainRequireService {

    @Autowired
    private IEmpTrainRequireDAO empTrainRequireDAO;

    @Autowired
    private ICompanyService companyService;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpTrainRequireVO save(EmpTrainRequireAddDTO empTrainRequireAddDTO, TokenUser tokenUser) {
        // 校验是否存在
        EmpTrainRequireVO requireVO = findByCompanyAndType(
                empTrainRequireAddDTO.getCompanyId(),
                empTrainRequireAddDTO.getRequireType(),
                tokenUser.getSiteId());
        if (!Objects.isNull(requireVO)) {
            // 修改
            EmpTrainRequireUpdateDTO requireUpdateDTO = empTrainRequireAddDTO.to(EmpTrainRequireUpdateDTO.class);
            requireUpdateDTO.setId(requireVO.getId());

            update(requireUpdateDTO, tokenUser);
            return selectById(requireVO.getId(), tokenUser.getSiteId());
        }
        // 转DO
        EmpTrainRequireDO empTrainRequireDO = empTrainRequireAddDTO.to(EmpTrainRequireDO.class);
        // 保存
        empTrainRequireDO = save(empTrainRequireDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return empTrainRequireDO.to(EmpTrainRequireVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(EmpTrainRequireUpdateDTO empTrainRequireUpdateDTO, TokenUser tokenUser) {
        //转DO
        EmpTrainRequireDO empTrainRequireDO = empTrainRequireUpdateDTO.to(EmpTrainRequireDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(empTrainRequireDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... empTrainRequireIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) empTrainRequireIds);
    }


    @Override
    public EmpTrainRequireVO selectById(String empTrainRequireId, String siteId) {
        EmpTrainRequireDO empTrainRequireDO = selectDOById(empTrainRequireId, siteId);
        if (empTrainRequireDO == null) {
            return null;
        }
        return empTrainRequireDO.to(EmpTrainRequireVO.class);
    }

    @Override
    public List<EmpTrainRequireListVO> select(EmpTrainRequireQuery empTrainRequireQuery, String siteId) {
        List<EmpTrainRequireDO> empTrainRequireDOList = selectDO(empTrainRequireQuery, siteId);
        if(CollectionUtils.isEmpty(empTrainRequireDOList)) {
            return empTrainRequireDOList == null ? null : new ArrayList<>();
        }
        return empTrainRequireDOList.stream()
                .map(empTrainRequireDO -> empTrainRequireDO.to(EmpTrainRequireListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(EmpTrainRequireQuery empTrainRequireQuery, String siteId) {
        Example example = buildExample(empTrainRequireQuery,siteId);
        return empTrainRequireDAO.selectCountByExample(example);
    }

    @Override
    public Page<EmpTrainRequireListVO> selectPage(EmpTrainRequireQuery empTrainRequireQuery, String siteId) {
        Example example = buildExample(empTrainRequireQuery,siteId);
        Page<EmpTrainRequireDO> page = empTrainRequireDAO.selectPageByExample(example,
                empTrainRequireQuery.getPageNo(),
                empTrainRequireQuery.getPageSize());

        return page.to(EmpTrainRequireListVO.class);
    }

    @Override
    public EmpTrainRequireDO selectDOById(String empTrainRequireId, String siteId) {
        return listById(empTrainRequireId, siteId);
    }

    @Override
    public List<EmpTrainRequireDO> selectDO(EmpTrainRequireQuery empTrainRequireQuery, String siteId) {
        Example example = buildExample(empTrainRequireQuery,siteId);
        return empTrainRequireDAO.selectByExample(example);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpTrainRequireVO selectByCompanyAndType(String companyId, String requireType, TokenUser tokenUser) {
        EmpTrainRequireVO requireVO = findByCompanyAndType(companyId, requireType, tokenUser.getSiteId());
        if (Objects.isNull(requireVO)) {
            CompanyDO company = companyService.selectDOById(companyId, tokenUser.getSiteId());
            if (Objects.isNull(company)) {
                return null;
            }
            // 没有初始化一条
            EmpTrainRequireAddDTO requireAddDTO = new EmpTrainRequireAddDTO();
            requireAddDTO.setCompanyId(companyId);
            requireAddDTO.setRequireType(requireType);

            requireVO = save(requireAddDTO, tokenUser);
        }
        return requireVO;
    }

    /**
     * 根据公司id和类型查询所需信息
     *
     * @param companyId   公司id
     * @param requireType 类型
     * @param siteId      站点
     * @return 所需信息
     */
    private EmpTrainRequireVO findByCompanyAndType(String companyId, String requireType, String siteId) {
        Example example = new Example(EmpTrainRequireDO.class);
        example.and()
                .andEqualTo("companyId", companyId)
                .andEqualTo("requireType", requireType)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);

        EmpTrainRequireDO requireDO = empTrainRequireDAO.selectOneByExample(example);
        if (Objects.isNull(requireDO)) {
            return null;
        }
        return requireDO.to(EmpTrainRequireVO.class);
    }


    /**
     * 根据查询参数，构建example

     * @param empTrainRequireQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(EmpTrainRequireQuery empTrainRequireQuery, String siteId) {
        Example example = new Example(EmpTrainRequireDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (empTrainRequireQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", empTrainRequireQuery.getCompanyId());
        }
        if (empTrainRequireQuery.getRequireType() != null) {
            example.and().andEqualTo("requireType", empTrainRequireQuery.getRequireType());
        }
        if (empTrainRequireQuery.getTrainCourse() != null) {
            example.and().andEqualTo("trainCourse", empTrainRequireQuery.getTrainCourse());
        }
        if (empTrainRequireQuery.getStartTime() != null) {
            example.and().andEqualTo("startTime", empTrainRequireQuery.getStartTime());
        }
        if (empTrainRequireQuery.getEndTime() != null) {
            example.and().andEqualTo("endTime", empTrainRequireQuery.getEndTime());
        }
        if (empTrainRequireQuery.getTrainDuration() != null) {
            example.and().andEqualTo("trainDuration", empTrainRequireQuery.getTrainDuration());
        }
        if (empTrainRequireQuery.getTrainOrgName() != null) {
            example.and().andEqualTo("trainOrgName", empTrainRequireQuery.getTrainOrgName());
        }
        if (empTrainRequireQuery.getAssessResult() != null) {
            example.and().andEqualTo("assessResult", empTrainRequireQuery.getAssessResult());
        }
        if (empTrainRequireQuery.getTrainAddress() != null) {
            example.and().andEqualTo("trainAddress", empTrainRequireQuery.getTrainAddress());
        }
        if (empTrainRequireQuery.getTrainMode() != null) {
            example.and().andEqualTo("trainMode", empTrainRequireQuery.getTrainMode());
        }
        // 排序
        ExampleKit.setExampleOrder(example,empTrainRequireQuery.getOrders());
        return example;
    }
}




